<template>
  <div>
    <div class="btn-box">
      <el-button type="success" @click="addSubject(0,'')" size="small">添加</el-button>
      <el-button type="danger" @click="deleteAll" size="small">删除全部</el-button>
      <el-button type="primary" @click="dialogFormVisible = true" size="small">批量导入</el-button>
    </div>

    <div class="data-box">
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

        <el-table-column
          prop="sort"
          label="排序号"
          sortable
          width="120">
        </el-table-column>

        <el-table-column
          prop="title"
          label="分类名称"
          sortable
          width="180">
        </el-table-column>

        <el-table-column
          prop="gmtCreate"
          label="创建时间"
          sortable
          width="180">
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <div style="display: flex; justify-content: flex-end; width: 180px">
              <el-button v-if="scope.row.parentId == 0" type="success" size="mini"
                         @click="addSubject(scope.row.id,scope.row.title)">
                添加
              </el-button>
              <el-button type="primary" size="mini" @click="alterSubject(scope.row)">修改</el-button>
              <el-button type="danger" size="mini" @click="deleteSubject(scope.row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>

      </el-table>

    </div>

    <!-- Form -->
    <el-dialog title="批量导入课程分类" :visible.sync="dialogFormVisible">
      <span>文件名称后出现绿色勾即代表导入完毕</span>
      <span class="a-class">
        <a href="https://alan-guli.oss-cn-shenzhen.aliyuncs.com/excel/subjec-import.xlsx">点击下载导入模板</a>
      </span>

      <el-upload
        class="upload-demo"
        action="http://127.0.0.1:8001/eduService/subject"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="3"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
    </el-dialog>

    <!-- Form -->
    <el-dialog :title="title" :visible.sync="dialogChild">
      <el-form :model="subject">
        <el-form-item v-if="subject.pname" label="父级名称" :label-width="formLabelWidth">
          <el-input v-model="subject.pname" autocomplete="off" disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-input v-model="subject.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序号" :label-width="formLabelWidth"
                      prop="sort"
                      :rules="[
                                  { required: true, message: '排序号不能为空'},
                                  { type: 'number', message: '排序号必须为数字值'}
                                ]"
        >
          <el-input v-model.number="subject.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChild = false">取 消</el-button>
        <el-button type="primary" @click="updateSubject">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
  .a-class {
    display: block;
    margin: 20px 0;
    color: #409EFF;
  }

  .btn-box {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
    margin: 5px 15px 5px 15px;
    border: #f4f4f5 1px solid;
  }

  .data-box {
    border: 1px solid #f4f4f5;
    margin: 10px;
  }
</style>
<script>

  import subject from "@/api/edu/subject";

  export default {
    data() {
      return {
        tableData: [],
        dialogFormVisible: false,
        fileList: [],

        subject: {},
        dialogChild: false,
        formLabelWidth: '100px',
        title: ''
      }
    },
    methods: {
      // 01 获取所有的列表
      getList() {
        subject.getAll().then(response => {
          this.tableData = response.data.list;
        })
      },

      // 02 删除分类
      deleteSubject(id) {
        this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          subject.deleteSubject(id).then(response => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      // 05 添加子级
      addSubject(pid, pname) {
        this.subject = {
          parentId: pid,
          pname: pname
        }
        this.dialogChild = true
        if (pid == 0) {
          this.title = "添加课程分类"
        } else {
          this.title = "添加子级课程分类"
        }
      },
      saveChild() {
        subject.saveChild(this.subject).then(response => {
          this.subject.parentId = 0
          this.dialogChild = false
          this.getList()
          this.$message({
            type: 'success',
            message: '保存成功!',
          });
        })
      },

      // 04 修改
      alterSubject(row) {
        this.dialogChild = true
        this.subject = {...row}
        if (row.parentId == 0) {
          this.title = "修改课程分类"
        } else {
          this.title = "修改子级课程分类"
        }
      },
      updateSubject() {
        subject.update(this.subject).then(response => {
          this.dialogChild = false
          this.getList()
          this.$message({
            type: 'success',
            message: '修改成功!',
          });
        })
      },

      // 03 文件上传
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },

      // 04 删除全部
      deleteAll() {
        this.$confirm('确定删除全部课程分类?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          subject.deleteAll().then(response => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
    // 监听属性
    watch: {
      dialogFormVisible(newVal, oldVal) {
        if (oldVal) {
          this.getList()
        }
      }
    },
    created() {
      this.getList()
    },
  }
</script>
